import 'package:flutter/material.dart';

/// 复选框-含标题
class QCheckBox extends StatefulWidget {
  final String title;
  final double? fontSize;
  final double width;
  final String? icon;
  final int index;
  final Function? tapCallback;

  const QCheckBox({
    Key? key,
    required this.width,
    required this.index,
    this.icon,
    this.title = "",
    this.fontSize,
    this.tapCallback,
  }) : super(key: key);

  @override
  _QCheckBoxState createState() => _QCheckBoxState();
}

class _QCheckBoxState extends State<QCheckBox> {
  bool isSure = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          isSure = !isSure;
        });
        if (widget.tapCallback != null) {
          widget.tapCallback!(widget.index);
        }
      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            width: widget.width,
            height: widget.width,
            decoration: BoxDecoration(
              border: Border.all(
                  width: 1,
                  color: isSure
                      ? const Color(0xFF177FF3)
                      : const Color.fromRGBO(151, 151, 151, 1)),
              borderRadius: BorderRadius.circular(4),
              color: isSure ? const Color(0xFF177FF3) : Colors.transparent,
            ),
            child: Offstage(
              offstage: isSure ? false : true,
              child: widget.icon == null
                  ? const Icon(
                      Icons.check,
                      size: 10,
                      color: Colors.white,
                    )
                  : Image.asset(widget.icon!,
                      color: Colors.white,
                      width: widget.width,
                      height: widget.width),
            ),
          ),
          Container(
            padding: const EdgeInsets.only(left: 5),
            child: Text(
              widget.title,
              style: TextStyle(
                  fontSize: widget.fontSize ?? 14, color: const Color.fromRGBO(96, 105, 114, 1)),
            ),
          )
        ],
      ),
    );
  }
}
